﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace AHCS
{
    public class Encryption
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Pass"></param>
        /// <returns></returns>
        public static string EncryPW(string Pass)
        {
            return DesEncrypt(Pass, "konghaoxue");
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="strPass"></param>
        /// <returns></returns>
        public static string DisEncryPW(string strPass)
        {
            return DesDecrypt(strPass, "konghaoxue");
        }



        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="encryptString"></param>
        /// <returns></returns>
        public static string DesEncrypt(string encryptString, string key)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="decryptString"></param>
        /// <returns></returns>
        public static string DesDecrypt(string decryptString, string key)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }


    }


    class TimeClass
    { 

        /*CPUid*/
        public static string GetCpuId()
        {
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();

            string strCpuID = null;
            foreach (ManagementObject mo in moc)
            {
                strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                break;
            }
            return strCpuID;
        }

     
        

        /*写入注册表*/
        public static void WriteSetting(string Key, string Setting)  // name = key  value=setting  Section= path
        {
           // string text1 = Section;
            RegistryKey key1 = Registry.CurrentUser.CreateSubKey("Software\\CangeShen\\anhui"); // .LocalMachine.CreateSubKey("Software\\mytest");
            if (key1 == null)
            {
                return;
            }
            key1.SetValue(Key, Setting);
            key1.Close();


        }

        /*读取注册表*/
        public static string ReadSetting( string Key, string Default)
        {
            if (Default == null)
            {
                Default = "";
            }
          //  string text2 = Section;
            RegistryKey key1 = Registry.CurrentUser.OpenSubKey("Software\\CangeShen\\anhui");
            if (key1 != null)
            {
                object obj1 = key1.GetValue(Key, Default);
                key1.Close();
               
                return obj1.ToString(); ;
            }
            return Default;
        }

        public static List<string> ReadSettingAll()
        {

            List<string> liststr = new List<string>();
            //  string text2 = Section;
            RegistryKey key1 = Registry.CurrentUser.OpenSubKey("Software\\CangeShen\\anhui");
            if (key1 != null)
            {
               string[] keystr=  key1.GetValueNames();
                for (int i = 0; i < key1.ValueCount; i++)
                {
                  object ob=   key1.GetValue(keystr[i]);
                    liststr.Add(ob.ToString());
                }
              
                key1.Close();
;
            }
            return liststr;
        }
    }

}
